﻿using UtilPublic.ServerLoger;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UtilPublic.Telnet
{
    /// <summary>
    /// Telnet服务器
    /// </summary>
    public static class TelnetServer
    {
        /// <summary>
        /// 
        /// </summary>
        public static string Name = "Telnet服务器";

        /// <summary>
        /// 服务
        /// </summary>
        public static AppServer ThisTelnetServer = new AppServer();
        /// <summary>
        /// 消息锁
        /// </summary>
        public static object LockerMessage = new object();
        /// <summary>
        /// 消息队列
        /// </summary>
        public static List<TelnetMessage> ListMessage = new List<TelnetMessage>();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="port"></param>
        public static void ThreadTelnetServer(int port)
        {
            ThisTelnetServer = new AppServer();

            var serConfig = new ServerConfig();
            serConfig.Port = port;
            serConfig.TextEncoding = "UTF-8";

            //Setup the appServer
            //if (!ThisTelnetServer.Setup(IniConf.ConfSwitch.TelnetServerPort)) //Setup with listening port
            if (!ThisTelnetServer.Setup(serConfig)) //Setup with listening port
            {
                loger.Fatal($"初始化 {Name}:{serConfig.Port} 失败");
                return;
            }

            ThisTelnetServer.NewSessionConnected += new SessionHandler<AppSession>(TelnetServer_NewSessionConnected);
            ThisTelnetServer.SessionClosed += new SessionHandler<AppSession, CloseReason>(TelnetServer_SessionClosed);
            ThisTelnetServer.NewRequestReceived += new RequestHandler<AppSession, StringRequestInfo>(TelnetServer_NewRequestReceived);

            Console.WriteLine();

            //Try to start the appServer
            if (!ThisTelnetServer.Start())
            {
                loger.Fatal($"启动 {Name}:{serConfig.Port} 失败");
                return;
            }

            loger.Info($"启动 {Name}:{serConfig.Port} 成功");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="session"></param>
        static void TelnetServer_NewSessionConnected(AppSession session)
        {
            loger.Debug($"接收到Telnet连接: {session.SessionID} {session.LocalEndPoint.ToString()} {session.RemoteEndPoint.ToString()}");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="session"></param>
        /// <param name="reason"></param>
        static void TelnetServer_SessionClosed(AppSession session, CloseReason reason)
        {
            loger.Debug($"断开Telnet连接: {session.SessionID} {session.LocalEndPoint.ToString()} {session.RemoteEndPoint.ToString()}");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="session"></param>
        /// <param name="requestInfo"></param>
        static void TelnetServer_NewRequestReceived(AppSession session, StringRequestInfo requestInfo)
        {
            //loger.Debug($"接收到Telnet消息: Key:{requestInfo.Key}");

            lock (LockerMessage)
            {
                ListMessage.Add(new TelnetMessage() { Session = session, RequestInfo = requestInfo });
            }

        }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="SessionID"></param>
        /// <param name="Key"></param>
        /// <param name="message"></param>
        public static void Send(string SessionID, string Key, string message)
        {
            var Session = ThisTelnetServer.GetSessionByID(SessionID);
            if (Session != null)
            {
                loger.Debug($"发送Telnet数据: {Key} {message}");
                Session.Send($"{Key} {message}");
            }
        }
    }
    /// <summary>
    /// 消息体
    /// </summary>
    public class TelnetMessage
    {
        /// <summary>
        /// 
        /// </summary>
        public AppSession Session;
        /// <summary>
        /// 
        /// </summary>
        public StringRequestInfo RequestInfo;
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="message"></param>
        public void Send(string message)
        {
            loger.Debug($"发送Telnet数据: {RequestInfo.Key} {message}");
            Session.Send($"{RequestInfo.Key} {message}");
        }
    }

}
